package com.example.styservice.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.styinterface.interfaces.IAdminService;
import com.example.styinterface.utils.R;
import com.example.stymodel.model.Admin;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wcq
 */
@Component
@Service //这里的Service是Dubbo的
public class AdminServiceImpl implements IAdminService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public String helloAdmin(String name) {
        return "Hello " + name;
    }

    @Override
    public R insert(Admin admin) {
        //插入重复数据
        //　　insert: 若新增数据的主键已经存在，则会抛 org.springframework.dao.DuplicateKeyException 异常提示主键重复，不保存当前数据。
        //　　save: 若新增数据的主键已经存在，则会对当前已经存在的数据进行修改操作。
        //
        //批操作
        //　　insert: 可以一次性插入一整个列表，而不用进行遍历操作，效率相对较高
        //　　save: 需要遍历列表，进行一个个的插入
        mongoTemplate.save(admin);
        return R.success(200,"插入成功");

    }

    @Override
    public R update(Admin admin) {
        Query query = new Query(Criteria.where("id").is(admin.getId()));
        Admin admin1=mongoTemplate.findOne(query,Admin.class);
        if(admin1==null){
            return R.success(200,"未找到信息");
        }
        Update update = new Update().set("name",admin.getName()).set("tel",admin.getTel()).set("email",admin.getEmail())
                .set("position",admin.getPosition()).set("remark",admin.getRemark());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, Admin.class);
        if (updateResult.getMatchedCount() == 0) {
            return R.success(200,"未找到该ID的Admin信息");
        }
        return R.success(200,"更新成功");
    }

    @Override
    public R delete(String id) {
        // 查找其id字段的值与传入参数id相等的数据
        Query query = new Query(Criteria.where("id").is(id));

        DeleteResult deleteResult=mongoTemplate.remove(query,Admin.class);
        if(deleteResult.getDeletedCount()==0){
            return R.success(200,"未找到该ID的Admin信息");
        }
        return R.success(200,"删除成功");
    }

    @Override
    public R selectAll() {
        Query query = new Query();
        //指定排序字段 id
        query.with(Sort.by(Sort.Direction.ASC,"id"));
        List<Admin> adminList=mongoTemplate.find(query,Admin.class);

        return R.success(adminList,200,"查找成功");
    }

    //ById
    @Override
    public R selectOne(String id) {
        Query query = new Query(Criteria.where("id").is(id));
        Admin admin= mongoTemplate.findOne(query,Admin.class);
        if(admin==null){
            return R.success(admin,200,"id不存在");
        }
        return R.success(admin,200,"查找成功");
    }
}
